<?php

namespace app\api\controller;
use think\Db;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        $this->success('请求成功');
    }


    //获取应用类型
    public function getAppCategory(){
        //系统服务
        $info['list1']=Db::table('member_app_type')->field('name,id')->select();

        //单一服务
        $info['list2']=Db::table('member_app_category')->where('develop_model', 2)->field('name as title,id,des as detail')->select();

        $this->success('返回成功', ['info' => $info]);
    }

    //获取应用行业
    public function getAppIndustry(){
        $list=Db::table('member_app_industry')->field('name,id')->select();
        $this->success('返回成功', ['info' => $list]);
    }


    //获取单一服务
    public function getSingle(){
        $cate_id=input('post.cate_id');
        //$cate_id=3;
        //独立部分
        $list['type_1']=Db::table('member_dev_price')->where(['develop_model'=>2, 'cate_id'=>$cate_id,'type'=>1])->select();
        //通用部分
        $list['type_2']=Db::table('member_dev_price')->where(['develop_model'=>2, 'cate_id'=>$cate_id,'type'=>2])->select();

        foreach ( $list['type_1'] as &$value){
            $value['version_price']=json_decode($value['version_price']);
           // $verison=Db::table('member_app_version')->where('id', $value['version_price'][0][0])->find();
        }

        foreach ($list['type_2'] as &$value){
            $value['version_price']=json_decode($value['version_price']);
        }

        /*dump($list);
        die();*/

        $this->success('返回成功', ['info' => $list]);


    }

    //获取单一服务价格
    public function getSinglePrice(){
       $post=input('post.');
       $checklist=json_decode($post['checklist'],true);

       $price=0;
       $versions=array();
       foreach ($checklist as $value){
           $price+=$value['price']*$value['num'];
           if(!in_array($value['version'],$versions)){
             $versions[]=$value['version'];
           }
       }
      /* dump($checlist);
       die();*/

       $info['price']=$price;
       $info['verison_count']=count($versions);
       $info['page_count']=count($checklist);
       $this->success('返回成功', ['info' => $info]);
    }

    public function savePrice(){
        $data=input('post.');
        $info=Db::table('member_baojia_info')->where("project_id",$data['project_id'])->find();
        if($info){
            $re=Db::table('member_baojia_info')->where('project_id', $data['project_id'])->update($data);
        }else{
            $re=Db::table('member_baojia_info')->insert($data);
        }
        if($re){
            $this->success('返回成功', ['info' => $re]);
        }else{
            $this->error('保存失败');
        }

    }




    public function Pinggu(){
        $hid=input('post.hid');

        //行业
        $industry=Db::table('member_app_industry')->where('id',$hid)->find();

        $type_select=input('post.select_type');

        $type_select=json_decode($type_select,true);

        $type_select_id=array_column($type_select,'id');

        //应用类型

        $app_cate=input('post.app_cate');

        if($app_cate==1){
            $type['version_id']="1,4,9";
            $type_id=$type_select_id;
        }else{
            $type_id=$app_cate;
            $type=Db::table('member_app_type')->where('id',$type_id)->find();
        }


        //每个应用包含的版本，用于选中
        $info['laguage_version']=explode(",",$type['version_id']);
        $info['laguage_version']=array_map("intval",$info['laguage_version']);

        //所有的应用版本
        $info['version']=Db::table('member_app_version')->where('id','in',$info['laguage_version'])->select();
        $info['version']=array_column($info['version'],null,'id');

        //应用逻辑
        $info['logic']=Db::table('member_app_logic')->select();
        $info['logic']['logic_index']=0;
        foreach ($info['logic'] as $key=>$value){
           if($industry['logic_id']!=0 && $value['id'] == $industry['logic_id']){
               $info['logic']['logic_index']=$key;
           }
        }

        //应用语言-功能开发
        $info['language_1']=Db::table('member_app_language')->where('cate_id','8')->field('name,id,des')->select();

        //前端开发
        $info['language_2']=Db::table('member_app_language')->where('cate_id','2')->field('name,id,des')->select();

        //ui设计
        $info['language_3']=Db::table('member_app_language')->where('cate_id','1')->field('name,id,des')->select();

        //板块的功能选择
        $info['gongnen']=Db::table('member_bankuai_technology_select')->where(array('tech_type'=>1,'type_id'=>array('in',$type_id)))->select();


        $industry['bid']=explode(",",$industry['bid']);

        foreach ($info['gongnen'] as $k=>&$value){
           $plate_gongnen=Db::table('member_plate_gongnen')->where('id',$value['gid'])->find();

           if(empty($plate_gongnen)){
               continue;
           }

           $page=Db::table('member_page')->where('id',$value['page_id'])->find();

           if($app_cate==1 && $page['admin_home']!=2){
               continue;
           }

           $child_plate=Db::table('member_plate')->where('id',$page['plate_id'])->find();
           if($child_plate['parent']==0){
               $plate=$child_plate;
               $child_plate['name']="无";
           }else{
               $plate=Db::table('member_plate')->where('id',$child_plate['parent'])->find();
           }

           //只获取当前行业下选中的板块下的功能
           if(!in_array($plate['id'],$industry['bid'])){
               continue;
           }

           $gongnen=Db::table('member_gongnen')->where('id', $plate_gongnen['gid'])->find();
           $child_gongnen=Db::table('member_gongnen_child')->where('id', $plate_gongnen['child_id'])->find();
           //模块
           $model=Db::table('member_model_manage')->where('id', $gongnen['model_id'])->find();

           //资源
           $res=Db::table('member_resource')->where(['child_gongnen'=>$child_gongnen['id'],'type'=>1])->find();
           //资源图片
           $res_file=Db::table('member_resource_file')->where(['r_id'=>$res['id']])->find();

           $value['page_name']=$page['name'];
           $value['child_name']=$child_plate['name'];
           $value['plate_name']=$plate['name'];
           $value['plate_id']=$plate['id'];
           $value['gongnen_name']=$gongnen['name'];
           $value['child_gongnen_name']=$child_gongnen['cname'];
            $value['child_gongnen_des']=$child_gongnen['des'];
           $value['gongnen_id']=$gongnen['id'];
           $value['child_gongnen_id']=$child_gongnen['id'];
           $value['technology']=json_decode($value['technology'],true);
           $value['technology']=array_column($value['technology'],1,0);
           $value['model_name']=$model['name'];
           $value['model_id']=$model['id'];
           $value['img']=(empty($res_file['path']))?'':"http://".$_SERVER['SERVER_NAME'].$res_file['path'];

           foreach ($info['language_1'] as $m => &$n) {
               if(array_key_exists($n['id'],$value['technology'])){
                 $value['hour']=$value['technology'][$n['id']];
                 $value['need']=0;
                 $n['gongnen'][]= $value;
               }
               
           }
        }

        foreach ($info['language_1'] as $key=>&$language_value){
            $gongnen_arr=[];
            if(!empty($language_value['gongnen'])){
                foreach ($language_value['gongnen'] as $ck=>$ch){
                    $gongnen_arr[$ch['plate_id']][]=$ch;
                }
            }
            $language_value['gongnen']=[];
            foreach ($gongnen_arr as $arr_key=>$arr_item){
                $language_value['gongnen']=array_merge($language_value['gongnen'],$arr_item);
            }
        }

        //当前行业选中的功能
        $industry['gong_id']=explode(",",$industry['gong_id']);
        $check_gongnen=[];
        //功能对应的页面id
        $check_page_id=[];

        if(!empty($info['language_1'][0]['gongnen'])){
            foreach ($info['language_1'][0]['gongnen'] as $key=>$gongnen){

                  if(in_array($gongnen['gid'],$industry['gong_id'])){
                      $iteam['index']=$key;
                      $iteam['hour']=$gongnen['hour'];
                      array_push($check_gongnen,$iteam);
                      $check_page_id[]=$gongnen['page_id'];
                  }
            }
        }

        $info['check_gongnen']=$check_gongnen;
        $info['check_gongnen_index']=array_column($check_gongnen,'index');


        //前端制作
        $info['qianduan']=Db::table('member_bankuai_technology_select')->where(array('tech_type'=>2,'type_id'=>array('in',$type_id)))->select();
        $qianduan_arr=[];
        foreach ($info['qianduan'] as $k2=>&$value2){
            $page=Db::table('member_page')->where('id',$value2['page_id'])->find();
            if(empty($page)){
                continue;
            }
            if($app_cate==1 && $page['admin_home']!=2){
                continue;
            }
            $child_plate=Db::table('member_plate')->where('id',$page['plate_id'])->find();
            if($child_plate['parent']==0){
                $plate=$child_plate;
                $child_plate['name']="无";
            }else{
                $plate=Db::table('member_plate')->where('id',$child_plate['parent'])->find();
            }

            if(!in_array($plate['id'],$industry['bid'])){
                continue;
            }

            $value2['page_name']=$page['name'];
            $value2['child_name']=$child_plate['name'];
            $value2['plate_name']=$plate['name'];
            $value2['plate_id']=$plate['id'];
            $value2['technology']=json_decode($value2['technology'],true);
            $value2['technology']=array_column($value2['technology'],1,0);


            if(empty($value2['technology'][0])){
                $value2['technology'][0]=0;
            }

            $qianduan_arr[$value2['plate_id']][]=$value2;
        }

        $info['qianduan']=[];
        foreach ($qianduan_arr as $q_key=>$q_value){
            $info['qianduan']=array_merge($info['qianduan'],$q_value);
        }

        foreach ($info['qianduan'] as $k3=>&$value3){
            foreach ( $info['language_2'] as $m => &$n) {
                if(array_key_exists($n['id'],$value3['technology'])){
                    $value3['hour_arr'][0]=$value3['technology'][0];
                    $value3['hour_arr'][1]=empty($value3['technology'][$n['id']])?0:$value3['technology'][$n['id']];
                    $value3['hour']=$value3['hour_arr'][0];
                    $value3['check_hour']=$value3['hour_arr'][0];
                    $value3['need']=1;
                    $n['qianduan'][$value3['gid']][]= $value3;
                }
            }
        }

        //选中的功能对应选中的页面
        $check_page=[];
        $default_version=$info['laguage_version'][0];
        if(!empty($info['language_2'][0]['qianduan'][$default_version])) {
            foreach ($info['language_2'][0]['qianduan'][$default_version] as $qianduan_key => $qianduan) {

                if (in_array($qianduan['page_id'],$check_page_id)) {
                    $iteam1['id']=$qianduan['page_id'];
                    $iteam1['index'] = $qianduan_key;
                    array_push($check_page, $iteam1);
                }
            }
        }
        $info['check_page']=$check_page;
        $info['check_page_index']=array_column($check_page,'index');



        $info['sheji']=Db::table('member_bankuai_technology_select')->where(array('tech_type'=>3,'type_id'=>array('in',$type_id)))->select();

        $sheji_arr=[];
        foreach ($info['sheji'] as $k4=>&$value4){
            $page=Db::table('member_page')->where('id',$value4['page_id'])->find();
            if(empty($page)){
                continue;
            }
            if($app_cate==1 && $page['admin_home']!=2){
                continue;
            }
            $child_plate=Db::table('member_plate')->where('id',$page['plate_id'])->find();
            if($child_plate['parent']==0){
                $plate=$child_plate;
                $child_plate['name']="无";
            }else{
                $plate=Db::table('member_plate')->where('id',$child_plate['parent'])->find();
            }

            if(!in_array($plate['id'],$industry['bid'])){
                continue;
            }

            $value4['page_name']=$page['name'];
            $value4['child_name']=$child_plate['name'];
            $value4['plate_name']=$plate['name'];
            $value4['plate_id']=$plate['id'];
            $value4['technology']=json_decode($value4['technology'],true);
            $value4['technology']=array_column($value4['technology'],1,0);

            $sheji_arr[$value4['plate_id']][]=$value4;


        }

        $info['sheji']=[];
        foreach ($sheji_arr as $s_key=>$s_value){
            $info['sheji']=array_merge($info['sheji'],$s_value);
        }

        foreach ($info['sheji'] as $k5=>&$value5){
            foreach ( $info['language_3'] as $m => &$n) {
                if(array_key_exists($n['id'],$value5['technology'])){
                    $value5['hour']=$value5['technology'][$n['id']];
                    $value5['need']=0;
                    $n['sheji'][$value5['gid']][]= $value5;
                }
            }
        }


        $dev_price=Db::table('member_dev_price')->where(['develop_model'=>1])->select();
        foreach ( $dev_price as $key=>&$vo){
            $vo['version_price']=json_decode($vo['version_price']);
            switch ($vo['name']) {
                case '系统架构':
                    $m='logic';
                    break;
                case '前端开发':
                    $m='qianduan';
                    break;
                case '功能开发':
                    $m='gongnen';
                    break;
                case '上线部署':
                    $m='bushu';
                    break;
                case 'ui设计':
                    $m='sheji';
                    break;
            }
            $info['dev_price'][$m]=$vo['version_price'][0][1];
        }



        unset($dev_price);
        unset($info['gongnen']);
        unset($info['qianduan']);
        unset($info['sheji']);


        $info['deploy']=Db::table('member_app_deploy')->field('id,name,cycle,tech_id')->select();
        $info['deploy']=array_column($info['deploy'],null,'tech_id');


        $this->success('返回成功', ['info' => $info]);
    }

    //获取资源
    public function getResource(){

        //功能资源
        $list=Db::table('member_resource')->where(['cate'=>8,'type'=>1])->select();
        $list=array_column($list,null,'child_gongnen');
        foreach ($list as $k=>&$value){
            $value['files']=Db::table('member_resource_file')->where(['r_id'=>$value['id']])->select();
            foreach ($value['files'] as &$file){
                $file['path']="http://".$_SERVER['SERVER_NAME'].$file['path'];
            }
            $gongnen_child=Db::table('member_gongnen_child')->where(['id'=>$value['child_gongnen']])->find();
            $value['des']=$gongnen_child['des'];
        }
        $this->success('返回成功', ['info' => $list]);
    }


    //获取资源
    public function getPageResource(){
        //功能资源
        $list=Db::table('member_resource')->where(['cate'=>10,'type'=>1])->select();
        $list=array_column($list,null,'page');
        foreach ($list as $k=>&$value){
            $value['files']=Db::table('member_resource_file')->where(['r_id'=>$value['id']])->select();
            foreach ($value['files'] as &$file){
                $file['path']="http://".$_SERVER['SERVER_NAME'].$file['path'];
            }
            $page=Db::table('member_page')->where(['id'=>$value['page']])->find();
            $value['des']=$page['des'];
        }
        $this->success('返回成功', ['info' => $list]);
    }


    public function Pinggu_copy(){
        //应用类型
        $info['type']=Db::table('member_app_type')->field('name,id')->select();
        //应用逻辑
        $info['logic']=Db::table('member_app_logic')->select();

        //应用语言-功能开发
        $info['language_1']=Db::table('member_app_language')->where('cate_id','8')->field('name,id,des')->select();

        //前端开发
        $info['language_2']=Db::table('member_app_language')->where('cate_id','2')->field('name,id,des')->select();

        //ui设计
        $info['language_3']=Db::table('member_app_language')->where('cate_id','1')->field('name,id,des')->select();

        $info['pages_zhizuo']=array();
        $info['pages_sheji']=array();
        //板块
        $info['plate']=Db::table('member_plate')->where('parent',0)->field('name,id')->select();
        foreach ($info['plate'] as $k=>&$value){
            //子版块
            $value['child']=Db::table('member_plate')->where('parent',$value['id'])->field('name,id')->select();
            $value['count']=0;

            foreach ($value['child'] as $key=>&$ch){
                //页面
                $ch['page']=Db::table('member_page')->where('plate_id',$ch['id'])->field('name,id')->select();
                $ch['count']=0;
                if(empty($ch['page'])){
                    unset($value['child'][$key]);
                   continue;
                }
                foreach ($ch['page'] as &$pa){
                    //前端制作页面
                    $page_1['name']=$pa['name'];
                    $page_1['id']=$pa['id'];
                    $page_1['techenology_select']=Db::table('member_bankuai_technology_select')->where(array('page_id'=>$pa['id'],'tech_type'=>2))->field('technology,tech_type')->find();
                    $page_1['childe_plate_name']=$ch['name'];
                    $page_1['plate_name']=$value['name'];
                     !empty($page_1['techenology_select'])?$page_1['techenology_select']['technology']=json_decode($page_1['techenology_select']['technology'],true):'';

                     array_push($info['pages_zhizuo'], $page_1);


                    $page_2['name']=$pa['name'];
                    $page_2['id']=$pa['id'];
                    $page_2['techenology_select']=Db::table('member_bankuai_technology_select')->where(array('page_id'=>$pa['id'],'tech_type'=>3))->field('technology,tech_type')->find();
                    $page_2['childe_plate_name']=$ch['name'];
                    $page_2['plate_name']=$value['name'];
                    !empty($page_2['techenology_select'])?$page_2['techenology_select']['technology']=json_decode($page_2['techenology_select']['technology'],true):'';

                    array_push($info['pages_sheji'], $page_2);


                    $pa['count']=0;
                    //功能
                    $pa['gong']=Db::table('member_plate_gongnen')
                                ->alias('a')
                               ->join('member_gongnen b','a.gid = b.id')->where('a.page_id',$pa['id'])->field('a.id,a.child_id,a.gid,b.name')->select();
                     
                     if(empty($pa['gong'])){
                        unset($value['child'][$key]);
                        continue;
                     }
                    foreach ($pa['gong'] as &$gn){

                        //子功能
                        $gn['child_gong']=Db::table('member_gongnen_child')->where('gid',$gn['gid'])->field('cname,id')->select();

                        $value['count']+=count($gn['child_gong']);
                        $ch['count']+=count($gn['child_gong']);
                        $pa['count']+=count($gn['child_gong']);
                        $gn['count']=count($gn['child_gong']);

                    }
                }
            }


            if(empty($value['child'])){
                unset($info['plate'][$k]);
            }

        }

        /*dump($info['plate']);
        die();*/
        $info['deploy']=Db::table('member_app_deploy')->field('id,name,cycle')->select();

        $this->success('返回成功', ['info' => $info]);
    }



    public function login(){
        if ($this->request->isPost()) {
            $mobile = $this->request->post('mobile');
            $password = $this->request->post('password');
            $rule = [
                'mobile'  => ['/^1[34578]\d{9}$/'],
                'password'   => 'require',
            ];

            $msg = [
                'password.require' => '密码必须',
                'mobile'     => '手机格式不正确',
            ];

            $data = [
                'mobile'  => $mobile,
                'password'  => $password,
            ];

            $validate = new Validate($rule, $msg);
            $result   = $validate->check($data);
            if (!$result) {
                $this->error($validate->getError());
            }

        }
    }


    public function register(){
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                $params['salt'] = Random::alnum();
                $params['password'] = md5(md5($params['password']) . $params['salt']);
                $params['createtime'] = time(); //设置新管理员默认头像。

                Db::table('member_user')->insert($params);
                $this->success('注册成功');
            }
            $this->error('注册失败');
        }
    }

}
